package cn.gok.day4;

import java.io.*;

/**
 * @ClassName StudyFile
 * @Description TODO
 * @Author HP
 * @Date 14:57 2022/7/7
 * @Version 1.0
 **/
public class StudyFile {
    public static void showDie(File file){
        File file1[]=file.listFiles();
        for(File f:file1){
            if(f.isFile()){
                System.out.println("文件绝对路径："+f.getAbsolutePath());
            }else if (f.isDirectory()){
                showDie(f);
                System.out.println("目录绝对路径："+f.getAbsolutePath());
            }
        }
    }
    public static void showByteInput() throws IOException {
        InputStream input=new FileInputStream("D:\\Oscar\\Code\\test2.txt");
        byte b[]=new byte[3];
        int len=0;
        String str=null;
        while ((len=input.read(b))!=-1){
            str=new String(b,0,len);
            System.out.println(str);
        }
        input.close();
    }
    public static void showByteOutput() throws IOException{
        OutputStream os=new FileOutputStream("D:\\Oscar\\Code\\test2.txt");

//        复制
        InputStream input=new FileInputStream("D:\\Oscar\\Code\\test.txt");
        byte b[]=new byte[3];
        int len=0;
        while ((len=input.read(b))!=-1){
            os.write(b,0,len);
        }
        os.write(45);
        os.write(90);
        os.write(92);
//        关闭资源
        os.close();
        input.close();

    }
//    字符流
    public static void writeStr() throws IOException {
        Writer writer=new FileWriter("D:\\Oscar\\Code\\test2.txt");
        writer.write("你好，这是一个字符输出流");
        writer.write(192);
        char c[]={'a','b','c'};
        writer.write(c);
        writer.close();
    }
    public static void readerStr() throws IOException {
        Reader reader=new FileReader("D:\\Oscar\\Code\\test2.txt");
        char c[]=new char[3];
        int len=0;
        while ((len=reader.read(c))!=-1){
            System.out.println(new String(c,0,len));
        }
    }
//    字符缓冲流
    public static void bufferString() throws IOException {
        BufferedReader bfr=new BufferedReader(new FileReader("D:\\Oscar\\Code\\test2.txt"));
        BufferedWriter bfw=new BufferedWriter(new FileWriter("D:\\Oscar\\Code\\test.txt"));
        String line="";
        while ((line=bfr.readLine())!=null){
            bfw.write(line);
            bfw.newLine();//换行
        }
        bfw.close();
        bfr.close();
    }
//    转换输出流和转换输入流  这里可以转换编码格式
    public static void utfFile() throws IOException {
        InputStreamReader is=new InputStreamReader(new FileInputStream("D:\\Oscar\\Code\\test.txt"),"utf-8");
        OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream("D:\\Oscar\\Code\\test2.txt"),"utf-8");
        char c[]=new char[10];
        int len=0;
        while ((len= is.read(c))!=-1){
            System.out.println(new String(c,0,len));
//            写入texe2
            os.write(c,0,len);
        }

//        关闭资源
        os.close();
        is.close();

    }
    public static void main(String[] args) throws IOException {
//        File file=new File("D:\\Oscar\\Code\\Code1\\src");
//        showDie(file);
//        showByteInput();
//        showByteOutput();
//        showByteInput();
//        writeStr();
//        readerStr();
//        bufferString();
        utfFile();

    }

}
